package com.ruoyi.service.utils;

import com.ruoyi.basics.domain.Point;
import com.ruoyi.basics.domain.User;
import com.ruoyi.basics.service.IPointService;
import com.ruoyi.basics.service.IUserService;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.service.domain.*;
import com.ruoyi.service.service.*;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import static java.math.BigDecimal.ROUND_DOWN;

/**
 * Created by admin on 2019/1/3.
 */
@Component
public class ServiceUtils {

    private Basics basics;


    public Basics getBasics() {
        return basics;
    }

    public void setBasics(Basics basics) {
        this.basics = basics;
    }

    public void insertDataByPoint(Point point) {
        if (basics == null)
            throw new BusinessException("后台数据错误,请联系管理员");
        trade(point);
        surplus(point);
        abundantWater(point);
        keep(point);
        execute(point);
        economize(point);
        assume(point);
        reality(point);
        discount(point);
    }

    @Autowired
    private IBasicsService basicsService;
    @Autowired
    private ITradeService tradeService;
    @Autowired
    private ISurplusService ISurplusService;
    @Autowired
    private IAbundantWaterService iAbundantWaterService;
    @Autowired
    private IKeepService iKeepService;
    @Autowired
    private IExecuteService iExecuteService;
    @Autowired
    private IEconomizeService iEconomizeService;
    @Autowired
    private IAssumeService assumeService;
    @Autowired
    private IDiscountService discountService;
    @Autowired
    private IRealityService realityService;
    @Autowired
    private IPointService pointService;
    @Autowired
    private IUserService userService;

    //数据字典
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 插入trade信息
     *
     * @param p
     */
    private void trade(Point p) {
        Trade trade = new Trade();
        trade.setPointId(p.getId());
        trade.setPointName(p.getName());
        trade.setBasicsId(basics.getId());
        trade.setAverageVolume(BigDecimal.ZERO);
        trade.setBasicsCost(BigDecimal.ZERO);
        trade.setBasicsPrice(BigDecimal.ZERO);
        trade.setDHAverage(BigDecimal.ZERO);
        trade.setDirectEle(BigDecimal.ZERO);
        trade.setEleAcount(BigDecimal.ZERO);
        trade.setFactoryPrice(BigDecimal.ZERO);
        trade.setFF(BigDecimal.ZERO);
        trade.setFP(BigDecimal.ZERO);
        trade.setFG(BigDecimal.ZERO);
        trade.setFTotal(BigDecimal.ZERO);
        trade.setPF(BigDecimal.ZERO);
        trade.setPP(BigDecimal.ZERO);
        trade.setPG(BigDecimal.ZERO);
        trade.setPTotal(BigDecimal.ZERO);
        trade.setKF(BigDecimal.ZERO);
        trade.setKP(BigDecimal.ZERO);
        trade.setKG(BigDecimal.ZERO);
        trade.setKTotal(BigDecimal.ZERO);
        trade.setVoltageGrade(p.getVoltageGrade());
        trade.setPriceDetailType(p.getPriceDetailType());
        trade.setJJAcount(BigDecimal.ZERO);
        trade.setJJAverage(BigDecimal.ZERO);
        trade.setSPAccount(BigDecimal.ZERO);
        trade.setSPPrice(BigDecimal.ZERO);
        trade.setTradeTotal(BigDecimal.ZERO);
        trade.setUpCost(BigDecimal.ZERO);
        tradeService.insertTrade(trade);
    }

    /**
     * 插入surplus信息
     *
     * @param p
     */
    private void surplus(Point p) {
        Surplus surplus = new Surplus();
        surplus.setPointName(p.getName());
        surplus.setPointId(p.getId());
        surplus.setBasicsId(basics.getId());
        surplus.setFF(BigDecimal.ZERO);
        surplus.setFP(BigDecimal.ZERO);
        surplus.setFG(BigDecimal.ZERO);
        surplus.setFTotal(BigDecimal.ZERO);
        surplus.setPF(BigDecimal.ZERO);
        surplus.setPP(BigDecimal.ZERO);
        surplus.setPG(BigDecimal.ZERO);
        surplus.setPTotal(BigDecimal.ZERO);
        surplus.setKF(BigDecimal.ZERO);
        surplus.setKP(BigDecimal.ZERO);
        surplus.setKG(BigDecimal.ZERO);
        surplus.setKTotal(BigDecimal.ZERO);
        surplus.setSurplusTotal(BigDecimal.ZERO);
        surplus.setUpAccount(BigDecimal.ZERO);
        surplus.setFactoryPrice(BigDecimal.ZERO);
        surplus.setSPAccount(BigDecimal.ZERO);
        surplus.setSPPrice(BigDecimal.ZERO);
        surplus.setJJAccount(BigDecimal.ZERO);
        surplus.setJJAverage(BigDecimal.ZERO);
        surplus.setEleAccount(BigDecimal.ZERO);
        surplus.setDHAverage(BigDecimal.ZERO);
        ISurplusService.insertSurplus(surplus);
    }

    /**
     * 插入abundantWater信息
     *
     * @param p
     */
    private void abundantWater(Point p) {
        AbundantWater abundantWater = new AbundantWater();
        abundantWater.setPointName(p.getName());
        abundantWater.setPointId(p.getId());
        abundantWater.setBasicsId(basics.getId());
        abundantWater.setFF(BigDecimal.ZERO);
        abundantWater.setFP(BigDecimal.ZERO);
        abundantWater.setFG(BigDecimal.ZERO);
        abundantWater.setFTotal(BigDecimal.ZERO);
        abundantWater.setUpAccount(BigDecimal.ZERO);
        abundantWater.setFactoryPrice(BigDecimal.ZERO);
        abundantWater.setSPAccount(BigDecimal.ZERO);
        abundantWater.setSPPrice(BigDecimal.ZERO);
        abundantWater.setJJAccount(BigDecimal.ZERO);
        abundantWater.setJJAverage(BigDecimal.ZERO);
        abundantWater.setEleAccount(BigDecimal.ZERO);
        abundantWater.setDHAverage(BigDecimal.ZERO);
        iAbundantWaterService.insertAbundantWater(abundantWater);
    }

    /**
     * 插入keep信息
     *
     * @param p
     */
    private void keep(Point p) {
        Keep keep = new Keep();
        keep.setPointName(p.getName());
        keep.setPointId(p.getId());
        keep.setBasicsId(basics.getId());
        keep.setFF(BigDecimal.ZERO);
        keep.setFP(BigDecimal.ZERO);
        keep.setFG(BigDecimal.ZERO);
        keep.setFTotal(BigDecimal.ZERO);
        keep.setPF(BigDecimal.ZERO);
        keep.setPP(BigDecimal.ZERO);
        keep.setPG(BigDecimal.ZERO);
        keep.setPTotal(BigDecimal.ZERO);
        keep.setKF(BigDecimal.ZERO);
        keep.setKP(BigDecimal.ZERO);
        keep.setKG(BigDecimal.ZERO);
        keep.setKTotal(BigDecimal.ZERO);
        keep.setKeepTotal(BigDecimal.ZERO);
        keep.setUpAccount(BigDecimal.ZERO);
        keep.setFactoryPrice(BigDecimal.ZERO);
        keep.setSPAccount(BigDecimal.ZERO);
        keep.setSPPrice(BigDecimal.ZERO);
        keep.setJJAccount(BigDecimal.ZERO);
        keep.setJJAverage(BigDecimal.ZERO);
        keep.setEleAccount(BigDecimal.ZERO);
        keep.setDHAverage(BigDecimal.ZERO);
        iKeepService.insertKeep(keep);
    }

    /**
     * 插入execute信息
     *
     * @param p
     */
    private void execute(Point p) {
        Execute execute = new Execute();
        execute.setPointName(p.getName());
        execute.setPointId(p.getId());
        execute.setBasicsId(basics.getId());
        execute.setFF(BigDecimal.ZERO);
        execute.setFP(BigDecimal.ZERO);
        execute.setFG(BigDecimal.ZERO);
        execute.setFTotal(BigDecimal.ZERO);
        execute.setPF(BigDecimal.ZERO);
        execute.setPP(BigDecimal.ZERO);
        execute.setPG(BigDecimal.ZERO);
        execute.setPTotal(BigDecimal.ZERO);
        execute.setKF(BigDecimal.ZERO);
        execute.setKP(BigDecimal.ZERO);
        execute.setKG(BigDecimal.ZERO);
        execute.setKTotal(BigDecimal.ZERO);
        execute.setEleTotal(BigDecimal.ZERO);
        execute.setUpAccount(BigDecimal.ZERO);
        execute.setFactoryPrice(BigDecimal.ZERO);
        execute.setSPAccount(BigDecimal.ZERO);
        execute.setSPPrice(BigDecimal.ZERO);
        execute.setJJAccount(BigDecimal.ZERO);
        execute.setJJAverage(BigDecimal.ZERO);
        execute.setEleAccount(BigDecimal.ZERO);
        execute.setDHAverage(BigDecimal.ZERO);
        iExecuteService.insertExecute(execute);
    }

    /**
     * 插入economize信息
     *
     * @param p
     */
    private void economize(Point p) {
        Economize economize = new Economize();
        economize.setPointName(p.getName());
        economize.setPointId(p.getId());
        economize.setBasicsId(basics.getId());
        economize.setFF(BigDecimal.ZERO);
        economize.setFP(BigDecimal.ZERO);
        economize.setFG(BigDecimal.ZERO);
        economize.setFTotal(BigDecimal.ZERO);
        economize.setPF(BigDecimal.ZERO);
        economize.setPP(BigDecimal.ZERO);
        economize.setPG(BigDecimal.ZERO);
        economize.setPTotal(BigDecimal.ZERO);
        economize.setKF(BigDecimal.ZERO);
        economize.setKP(BigDecimal.ZERO);
        economize.setKG(BigDecimal.ZERO);
        economize.setKTotal(BigDecimal.ZERO);
        economize.setEleTotal(BigDecimal.ZERO);
        economize.setUpAccount(BigDecimal.ZERO);
        economize.setFactoryPrice(BigDecimal.ZERO);
        economize.setSPAccount(BigDecimal.ZERO);
        economize.setSPPrice(BigDecimal.ZERO);
        economize.setJJAccount(BigDecimal.ZERO);
        economize.setJJAverage(BigDecimal.ZERO);
        economize.setEleAccount(BigDecimal.ZERO);
        economize.setDHAverage(BigDecimal.ZERO);
        iEconomizeService.insertEconomize(economize);
    }

    /**
     * 插入assume信息
     *
     * @param p
     */
    private void assume(Point p) {
        Assume assume = new Assume();
        assume.setPointName(p.getName());
        assume.setPointId(p.getId());
        assume.setBasicsId(basics.getId());
        assume.setFFP(BigDecimal.ZERO);
        assume.setFPP(BigDecimal.ZERO);
        assume.setFGP(BigDecimal.ZERO);
        assume.setFTotalP(BigDecimal.ZERO);
        assume.setPFP(BigDecimal.ZERO);
        assume.setPPP(BigDecimal.ZERO);
        assume.setPGP(BigDecimal.ZERO);
        assume.setPTotalP(BigDecimal.ZERO);
        assume.setKFP(BigDecimal.ZERO);
        assume.setKPP(BigDecimal.ZERO);
        assume.setKGP(BigDecimal.ZERO);
        assume.setKTotalP(BigDecimal.ZERO);
        assume.setEleAccountTotal(BigDecimal.ZERO);
        assume.setFF(BigDecimal.ZERO);
        assume.setFP(BigDecimal.ZERO);
        assume.setFG(BigDecimal.ZERO);
        assume.setFTotal(BigDecimal.ZERO);
        assume.setPF(BigDecimal.ZERO);
        assume.setPP(BigDecimal.ZERO);
        assume.setPG(BigDecimal.ZERO);
        assume.setPTotal(BigDecimal.ZERO);
        assume.setKF(BigDecimal.ZERO);
        assume.setKP(BigDecimal.ZERO);
        assume.setKG(BigDecimal.ZERO);
        assume.setKTotal(BigDecimal.ZERO);
        assume.setElePriceTotal(BigDecimal.ZERO);
        assume.setBasicsPrice(BigDecimal.ZERO);
        assume.setJJ(BigDecimal.ZERO);
        assume.setEleTotal(BigDecimal.ZERO);
        assume.setFFM(BigDecimal.ZERO);
        assume.setFPM(BigDecimal.ZERO);
        assume.setFGM(BigDecimal.ZERO);
        assume.setFTotalM(BigDecimal.ZERO);
        assume.setPFM(BigDecimal.ZERO);
        assume.setPPM(BigDecimal.ZERO);
        assume.setPGM(BigDecimal.ZERO);
        assume.setPTotalM(BigDecimal.ZERO);
        assume.setKFM(BigDecimal.ZERO);
        assume.setKPM(BigDecimal.ZERO);
        assume.setKGM(BigDecimal.ZERO);
        assume.setKTotalM(BigDecimal.ZERO);
        assumeService.insertAssume(assume);
    }

    /**
     * 插入reality信息
     *
     * @param p
     */
    private void reality(Point p) {
        Reality reality = new Reality();
        reality.setPointName(p.getName());
        reality.setPointId(p.getId());
        reality.setBasicsId(basics.getId());
        reality.setEleAccount(BigDecimal.ZERO);
        reality.setElePrice(BigDecimal.ZERO);
        reality.setEleF(BigDecimal.ZERO);
        reality.setJJPrice(BigDecimal.ZERO);
        reality.setZzJJPrice(BigDecimal.ZERO);
        reality.setDHPrice(BigDecimal.ZERO);
        reality.setDHAcount(BigDecimal.ZERO);
        realityService.insertReality(reality);
    }

    /**
     * 插入discount信息
     *
     * @param p
     */
    private void discount(Point p) {
        Discount discount = new Discount();
        discount.setPointName(p.getName());
        discount.setPointId(p.getId());
        discount.setBasicsId(basics.getId());
        discount.setEleAccount(BigDecimal.ZERO);
        discount.setBasicsAccount(BigDecimal.ZERO);
        discount.setZzJJ(BigDecimal.ZERO);
        discount.setTotal(BigDecimal.ZERO);
        discountService.insertDiscount(discount);
    }

    /**
     * 基本电价及直接交易
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public Trade getTradeId(Integer pointId, Integer basicsId) {
        Trade trade = new Trade();
        trade.setPointId(pointId);
        trade.setBasicsId(basicsId);
        List<Trade> trades = tradeService.selectTradeList(trade);
        if (CollectionUtils.isEmpty(trades)) {
            throw new BusinessException("后台数据错误,请联系管理员");
        }
        return trades.get(0);
    }

    /**
     * 富余电量
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public Surplus getSurplusId(Integer pointId, Integer basicsId) {
        Surplus surplus = new Surplus();
        surplus.setPointId(pointId);
        surplus.setBasicsId(basicsId);
        List<Surplus> surplus1 = ISurplusService.selectSurplusList(surplus);
        if (CollectionUtils.isEmpty(surplus1)) {
            throw new BusinessException("后台数据错误,请联系管理员");
        }
        return surplus1.get(0);
    }

    /**
     * 丰水期低谷时段弃水电量
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public AbundantWater getAbundantWaterId(Integer pointId, Integer basicsId) {
        AbundantWater abundantWater = new AbundantWater();
        abundantWater.setPointId(pointId);
        abundantWater.setBasicsId(basicsId);
        List<AbundantWater> abundantWater1 = iAbundantWaterService.selectAbundantWaterList(abundantWater);
        if (CollectionUtils.isEmpty(abundantWater1)) {
            throw new BusinessException("后台数据错误,请联系管理员");
        }
        return abundantWater1.get(0);
    }

    /**
     * 留存电量
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public Keep getKeepId(Integer pointId, Integer basicsId) {
        Keep keep = new Keep();
        keep.setPointId(pointId);
        keep.setBasicsId(basicsId);
        List<Keep> keep1 = iKeepService.selectKeepList(keep);
        if (CollectionUtils.isEmpty(keep1)) {
            throw new BusinessException("后台数据错误,请联系管理员");
        }
        return keep1.get(0);
    }

    /**
     * 执行目录电价（市场电量以外的电量）
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public Execute getExecuteId(Integer pointId, Integer basicsId) {
        Execute execute = new Execute();
        execute.setPointId(pointId);
        execute.setBasicsId(basicsId);
        List<Execute> execute1 = iExecuteService.selectExecuteList(execute);
        if (CollectionUtils.isEmpty(execute1)) {
            throw new BusinessException("后台数据错误,请联系管理员");
        }
        return execute1.get(0);
    }

    /**
     * 节能替代方案
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public Economize getEconomizeId(Integer pointId, Integer basicsId) {
        Economize economize = new Economize();
        economize.setPointId(pointId);
        economize.setBasicsId(basicsId);
        List<Economize> economize1 = iEconomizeService.selectEconomizeList(economize);
        if (CollectionUtils.isEmpty(economize1)) {
            throw new BusinessException("后台数据错误,请联系管理员");
        }
        return economize1.get(0);
    }

    /**
     * 假设执行政府定价
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public Assume getAssumeId(Integer pointId, Integer basicsId) {
        Assume assume = new Assume();
        assume.setPointId(pointId);
        assume.setBasicsId(basicsId);
        List<Assume> assume1 = assumeService.selectAssumeList(assume);
        if (CollectionUtils.isEmpty(assume1)) {
            throw new BusinessException("后台数据错误,请联系管理员");
        }
        return assume1.get(0);
    }

    /**
     * 实际执行情况合计
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public Reality getRealityId(Integer pointId, Integer basicsId) {
        Reality reality = new Reality();
        reality.setPointId(pointId);
        reality.setBasicsId(basicsId);
        List<Reality> reality1 = realityService.selectRealityList(reality);
        if (CollectionUtils.isEmpty(reality1)) {
            throw new BusinessException("后台数据错误,请联系管理员");
        }
        return reality1.get(0);
    }

    /**
     * 优惠电费
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public Discount getDiscountId(Integer pointId, Integer basicsId) {
        Discount discount = new Discount();
        discount.setPointId(pointId);
        discount.setBasicsId(basicsId);
        List<Discount> discount1 = discountService.selectDiscountList(discount);
        if (CollectionUtils.isEmpty(discount1)) {
            throw new BusinessException("后台数据错误,请联系管理员");
        }
        return discount1.get(0);
    }

    /**
     * 总的设置页面值
     *
     * @param mmap
     * @param pointId
     * @param basicsId
     */
    public void setModelMap(ModelMap mmap, Integer pointId, Integer basicsId) {
        mmap.put("trade", getTradeId(pointId, basicsId).getId());
        mmap.put("surplus", getSurplusId(pointId, basicsId).getId());
        mmap.put("abundantWater", getAbundantWaterId(pointId, basicsId).getId());
        mmap.put("keep", getKeepId(pointId, basicsId).getId());
        mmap.put("execute", getExecuteId(pointId, basicsId).getId());
        mmap.put("economize", getEconomizeId(pointId, basicsId).getId());
        mmap.put("assume", getAssumeId(pointId, basicsId).getId());
        mmap.put("reality", getRealityId(pointId, basicsId).getId());
        mmap.put("discount", getDiscountId(pointId, basicsId).getId());
    }

    /**
     * 总的获取,添加时单个时，须将其加入进来，方便后续查询
     * 获取各张表的ID
     *
     * @param pointId
     * @param basicsId
     * @return
     */
    public AjaxResult getInfo(Integer pointId, Integer basicsId) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("trade", getTradeId(pointId, basicsId).getId());
        ajaxResult.put("surplus", getSurplusId(pointId, basicsId).getId());
        ajaxResult.put("abundantWater", getAbundantWaterId(pointId, basicsId).getId());
        ajaxResult.put("keep", getKeepId(pointId, basicsId).getId());
        ajaxResult.put("execute", getExecuteId(pointId, basicsId).getId());
        ajaxResult.put("economize", getEconomizeId(pointId, basicsId).getId());
        ajaxResult.put("assume", getAssumeId(pointId, basicsId).getId());
        ajaxResult.put("reality", getRealityId(pointId, basicsId).getId());
        ajaxResult.put("discount", getDiscountId(pointId, basicsId).getId());
        return ajaxResult;
    }

    /**
     * @param pointId
     * @param basicsId
     */
    public void total(Integer pointId, Integer basicsId) {
        Trade trade = getTradeId(pointId, basicsId);//3.2
        Surplus surplus = getSurplusId(pointId, basicsId);//3.3
        AbundantWater abundantWater = getAbundantWaterId(pointId, basicsId);//3.4
        Keep keep = getKeepId(pointId, basicsId);//3.5
        Execute execute = getExecuteId(pointId, basicsId);//3.6
        Economize economize = getEconomizeId(pointId, basicsId);//3.7
        Assume assume = getAssumeId(pointId, basicsId);//3.8
        Reality reality = getRealityId(pointId, basicsId);//3.9
        Discount discount = getDiscountId(pointId, basicsId);//3.10
        //设置3.8的值
        setAssume(trade, surplus, abundantWater, keep, execute, economize, assume);
        setReality(trade, surplus, abundantWater, keep, execute, economize, reality);

        setDiscount(trade, assume, reality, discount);
    }

    //设置3.10
    private void setDiscount(Trade trade, Assume assume, Reality reality, Discount discount) {
        if (assume.getPPP().compareTo(BigDecimal.ZERO) == 0) {
            return;
        }
        discount.setEleAccount(assume.getElePriceTotal().subtract(reality.getEleF()).add(trade.getBasicsCost()));
        discount.setBasicsAccount(assume.getBasicsPrice().subtract(trade.getBasicsCost()));
        discount.setZzJJ(assume.getJJ().subtract(reality.getZzJJPrice()));
        discount.setTotal(discount.getEleAccount().add(discount.getBasicsAccount()).add(discount.getZzJJ()));
        discountService.updateDiscount(discount);
    }

    //设置3.9
    private void setReality(Trade trade, Surplus surplus, AbundantWater abundantWater, Keep keep, Execute execute, Economize economize, Reality reality) {
        //获取电量合计
        BigDecimal account_total = trade.getTradeTotal().add(surplus.getSurplusTotal()).add(abundantWater.getFTotal())
                .add(keep.getKeepTotal()).add(execute.getEleTotal()).add(economize.getEleTotal());
        reality.setEleAccount(account_total);
        //获取政府基金及附加金额
        BigDecimal jj_total = trade.getJJAcount().add(surplus.getJJAccount())
                .add(abundantWater.getJJAccount()).add(keep.getJJAccount()).add(execute.getJJAccount()).add(economize.getJJAccount());
        reality.setZzJJPrice(jj_total);
        //设置电费
        BigDecimal ele_total = trade.getEleAcount().add(surplus.getEleAccount()).add(abundantWater.getEleAccount()).add(keep.getEleAccount())
                .add(execute.getEleTotal()).add(economize.getEleTotal()).add(trade.getBasicsCost());
        reality.setEleF(ele_total);
        if (reality.getEleAccount().compareTo(BigDecimal.ZERO) == 0) {
            reality.setElePrice(BigDecimal.ZERO);
            reality.setJJPrice(BigDecimal.ZERO);
            reality.setDHAcount(BigDecimal.ZERO);
            reality.setDHPrice(BigDecimal.ZERO);
        } else {
            //设置电价
            reality.setElePrice(reality.getEleF().divide(reality.getEleAccount(), 6, RoundingMode.HALF_UP));
            //设置基金及附加单价
            reality.setJJPrice(reality.getZzJJPrice().divide(reality.getEleAccount(), 6, RoundingMode.HALF_UP));
            //设置到户电费
            reality.setDHAcount(reality.getZzJJPrice().add(reality.getEleF()));
            //设置到户电价
            reality.setDHPrice(reality.getDHAcount().divide(reality.getEleAccount(), 6, RoundingMode.HALF_UP));
        }
        realityService.updateReality(reality);
    }

    //设置3.8
    private void setAssume(Trade trade, Surplus surplus, AbundantWater abundantWater, Keep keep, Execute execute, Economize economize, Assume assume) {
        //丰水期电量
        BigDecimal ff = trade.getFF().add(surplus.getFF()).add(abundantWater.getFF()).add(keep.getFF()).add(execute.getFF()).add(economize.getFF());
        BigDecimal fp = trade.getFP().add(surplus.getFP()).add(abundantWater.getFP()).add(keep.getFP()).add(execute.getFP()).add(economize.getFP());
        BigDecimal fg = trade.getFG().add(surplus.getFG()).add(abundantWater.getFG()).add(keep.getFG()).add(execute.getFG()).add(economize.getFG());
        BigDecimal ftotal = ff.add(fp).add(fg);
        //平水期电量
        BigDecimal pf = trade.getPF().add(surplus.getPF()).add(keep.getPF()).add(execute.getPF()).add(economize.getPF());
        BigDecimal pp = trade.getPP().add(surplus.getPP()).add(keep.getPP()).add(execute.getPP()).add(economize.getPP());
        BigDecimal pg = trade.getPG().add(surplus.getPG()).add(keep.getPG()).add(execute.getPG()).add(economize.getPG());
        BigDecimal ptotal = pf.add(pp).add(pg);
        //枯水期电量
        BigDecimal kf = trade.getKF().add(surplus.getKF()).add(keep.getKF()).add(execute.getKF()).add(economize.getKF());
        BigDecimal kp = trade.getKP().add(surplus.getKP()).add(keep.getKP()).add(execute.getKP()).add(economize.getKP());
        BigDecimal kg = trade.getKG().add(surplus.getKG()).add(keep.getKG()).add(execute.getKG()).add(economize.getKG());
        BigDecimal ktotal = kf.add(kp).add(kg);
        BigDecimal total = ftotal.add(ptotal).add(ktotal);//总合计
        assume.setFF(ff);
        assume.setFFM(ff.multiply(assume.getFFP()));
        assume.setFP(fp);
        assume.setFPM(fp.multiply(assume.getFPP()));
        assume.setFG(fg);
        assume.setFGM(fg.multiply(assume.getFGP()));
        assume.setFTotal(ftotal);
        assume.setFTotalM(assume.getFFM().add(assume.getFPM()).add(assume.getFGM()));
        assume.setPF(pf);
        assume.setPFM(pf.multiply(assume.getPFP()));
        assume.setPP(pp);
        assume.setPPM(pp.multiply(assume.getPPP()));
        assume.setPG(pg);
        assume.setPGM(pg.multiply(assume.getPGP()));
        assume.setPTotal(ptotal);
        assume.setPTotalM(assume.getPFM().add(assume.getPPM()).add(assume.getPGM()));
        assume.setKF(kf);
        assume.setKFM(kf.multiply(assume.getKFP()));
        assume.setKP(kp);
        assume.setKPM(kp.multiply(assume.getKPP()));
        assume.setKG(kg);
        assume.setKGM(kg.multiply(assume.getKGP()));
        assume.setKTotal(ktotal);
        assume.setKTotalM(assume.getKFM().add(assume.getKPM()).add(assume.getKGM()));
        assume.setEleAccountTotal(total);
        assume.setBasicsPrice(trade.getBasicsCost());//设置基本电费
        assume.setJJ(trade.getJJAcount());//设置基金总金额

        assume.setElePriceTotal(assume.getFTotalM().add(assume.getPTotalM()).add(assume.getKTotalM()));//电费合计
        assume.setEleTotal(assume.getBasicsPrice().add(assume.getJJ()).add(assume.getElePriceTotal()));//合计

        assumeService.updateAssume(assume);
    }


    public AjaxResult export(List<Basics> list, String sheetName) {
        OutputStream out = null;
        XSSFWorkbook workbook = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        try {
//            Resource resource = new ClassPathResource("static/export/dgyyhmxb.xlsx");
            ClassPathResource resource = new ClassPathResource("static/export/dgyyhmxb.xlsx");
            workbook = new XSSFWorkbook(resource.getInputStream());
            Sheet sheet = workbook.getSheet("大工业用户结算情况明细表");//获取sheet
            int rowIndex = 10;//从11行开始赋值
            for (int i = 0; i < list.size(); i++) {
                Basics basics = list.get(i);
                //获取表计信息
                Trade trade1 = new Trade();
                trade1.setBasicsId(basics.getId());

                /****   基本电价及直接交易  ***/
                List<Trade> trades = tradeService.selectTradeList(trade1);
                if (CollectionUtils.isEmpty(trades)) {
                    throw new BusinessException("后台数据错误,请联系管理员");
                }
                List<String> pointIds = new ArrayList<>();
                for (Trade t : trades) {
                    pointIds.add(t.getPointId() + "");
                }
                List<Point> points = pointService.selectPointByIds(pointIds);
                User user = userService.selectUserById(Long.valueOf(basics.getUserId()));
                SysDept sysDept = deptService.selectDeptById(user.getDeptId());
                for (Point p : points) {//根据point来给excel赋值
                    AbundantWater abundantWater = getAbundantWaterId(p.getId(), basics.getId());
                    Assume assume = getAssumeId(p.getId(), basics.getId());
                    Discount discount = getDiscountId(p.getId(), basics.getId());
                    Economize economize = getEconomizeId(p.getId(), basics.getId());
                    Execute execute = getExecuteId(p.getId(), basics.getId());
                    Keep keep = getKeepId(p.getId(), basics.getId());
                    Reality reality = getRealityId(p.getId(), basics.getId());
                    Surplus surplus = getSurplusId(p.getId(), basics.getId());
                    Trade trade = getTradeId(p.getId(), basics.getId());
                    Row row = sheet.createRow(rowIndex);
                    setCellValue(row, 0, basics.getUser().getName());
                    setCellValue(row, 1, p.getCode());
                    setCellValue(row, 3, sysDept.getDeptName());
                    setCellValue(row, 4, p.getVoltageGrade(), "voltage_grade");
                    setCellValue(row, 5, p.getPriceDetailType(), "price_detail_type");

                    setCellValue(row, 6, format.format(basics.getResultDate()) + "第" + basics.getName() + "次");
                    /*****  基本电价及直接交易  ****/
                    setCellValue(row, 7, trade.getAverageVolume());
                    setCellValue(row, 8, trade.getBasicsPrice());
                    setCellValue(row, 9, trade.getBasicsCost());
                    setCellValue(row, 10, user.getUserType(), "user_type");

                    setCellValue(row, 11, trade.getFF());
                    setCellValue(row, 12, trade.getFP());
                    setCellValue(row, 13, trade.getFG());
                    setCellValue(row, 14, trade.getFTotal());
                    setCellValue(row, 15, trade.getPF());

                    setCellValue(row, 16, trade.getPP());
                    setCellValue(row, 17, trade.getPG());
                    setCellValue(row, 18, trade.getPTotal());
                    setCellValue(row, 19, trade.getKF());
                    setCellValue(row, 20, trade.getKP());

                    setCellValue(row, 21, trade.getKG());
                    setCellValue(row, 22, trade.getKTotal());
                    setCellValue(row, 23, trade.getTradeTotal());
                    setCellValue(row, 24, trade.getFactoryPrice());
                    setCellValue(row, 25, trade.getUpCost());

                    setCellValue(row, 26, trade.getSPPrice());
                    setCellValue(row, 27, trade.getSPAccount());
                    setCellValue(row, 28, trade.getJJAverage());
                    setCellValue(row, 29, trade.getJJAcount());
                    setCellValue(row, 30, trade.getDHAverage());

                    setCellValue(row, 31, trade.getEleAcount());


                    /*****  富余电量  ****/
                    setCellValue(row, 32, surplus.getFF());
                    setCellValue(row, 33, surplus.getFP());
                    setCellValue(row, 34, surplus.getFG());
                    setCellValue(row, 35, surplus.getFTotal());

                    setCellValue(row, 36, surplus.getPF());
                    setCellValue(row, 37, surplus.getPP());
                    setCellValue(row, 38, surplus.getPG());
                    setCellValue(row, 39, surplus.getPTotal());

                    setCellValue(row, 40, surplus.getKF());
                    setCellValue(row, 41, surplus.getKP());
                    setCellValue(row, 42, surplus.getKG());
                    setCellValue(row, 43, surplus.getKTotal());

                    setCellValue(row, 44, surplus.getSurplusTotal());
                    setCellValue(row, 45, surplus.getFactoryPrice());
                    setCellValue(row, 46, surplus.getUpAccount());
                    setCellValue(row, 47, surplus.getSPPrice());

                    setCellValue(row, 48, surplus.getSPAccount());
                    setCellValue(row, 49, surplus.getJJAverage());
                    setCellValue(row, 50, surplus.getJJAccount());
                    setCellValue(row, 51, surplus.getDHAverage());
                    setCellValue(row, 52, surplus.getEleAccount());

                    /*****  丰水期低谷时段弃水电量  ****/
                    setCellValue(row, 53, abundantWater.getFF());
                    setCellValue(row, 54, abundantWater.getFP());
                    setCellValue(row, 55, abundantWater.getFG());
                    setCellValue(row, 56, abundantWater.getFTotal());

                    setCellValue(row, 57, abundantWater.getFactoryPrice());
                    setCellValue(row, 58, abundantWater.getUpAccount());
                    setCellValue(row, 59, abundantWater.getSPPrice());
                    setCellValue(row, 60, abundantWater.getSPAccount());

                    setCellValue(row, 61, abundantWater.getJJAverage());
                    setCellValue(row, 62, abundantWater.getJJAccount());
                    setCellValue(row, 63, abundantWater.getDHAverage());
                    setCellValue(row, 64, abundantWater.getEleAccount());

                    /*****  留存电量  ****/
                    setCellValue(row, 65, keep.getFF());
                    setCellValue(row, 66, keep.getFP());
                    setCellValue(row, 67, keep.getFG());
                    setCellValue(row, 68, keep.getFTotal());

                    setCellValue(row, 69, keep.getPF());
                    setCellValue(row, 70, keep.getPP());
                    setCellValue(row, 71, keep.getPG());
                    setCellValue(row, 72, keep.getPTotal());

                    setCellValue(row, 73, keep.getKF());
                    setCellValue(row, 74, keep.getKP());
                    setCellValue(row, 75, keep.getKG());
                    setCellValue(row, 76, keep.getKTotal());

                    setCellValue(row, 77, keep.getKeepTotal());
                    setCellValue(row, 78, keep.getFactoryPrice());
                    setCellValue(row, 79, keep.getUpAccount());
                    setCellValue(row, 80, keep.getSPPrice());

                    setCellValue(row, 81, keep.getSPAccount());
                    setCellValue(row, 82, keep.getJJAverage());
                    setCellValue(row, 83, keep.getJJAccount());
                    setCellValue(row, 84, keep.getDHAverage());
                    setCellValue(row, 85, keep.getEleAccount());

                    /*****  执行目录电价（市场电量以外的电量）  ****/
                    setCellValue(row, 86, execute.getFF());
                    setCellValue(row, 87, execute.getFP());
                    setCellValue(row, 88, execute.getFG());
                    setCellValue(row, 89, execute.getFTotal());

                    setCellValue(row, 90, execute.getPF());
                    setCellValue(row, 91, execute.getPP());
                    setCellValue(row, 92, execute.getPG());
                    setCellValue(row, 93, execute.getPTotal());

                    setCellValue(row, 94, execute.getKF());
                    setCellValue(row, 95, execute.getKP());
                    setCellValue(row, 96, execute.getKG());
                    setCellValue(row, 97, execute.getKTotal());

                    setCellValue(row, 98, execute.getEleTotal());
                    setCellValue(row, 99, execute.getFactoryPrice());
                    setCellValue(row, 100, execute.getUpAccount());
                    setCellValue(row, 101, execute.getSPPrice());

                    setCellValue(row, 102, execute.getSPAccount());
                    setCellValue(row, 103, execute.getJJAverage());
                    setCellValue(row, 104, execute.getJJAccount());
                    setCellValue(row, 105, execute.getDHAverage());
                    setCellValue(row, 106, execute.getEleAccount());

                    /*****  节能替代  ****/
                    setCellValue(row, 107, user.getUseEleType(), "use_ele_type");
                    setCellValue(row, 108, user.getUseEleChangeType(), "use_ele_change_type");

                    setCellValue(row, 109, economize.getFF());
                    setCellValue(row, 110, economize.getFP());
                    setCellValue(row, 111, economize.getFG());
                    setCellValue(row, 112, economize.getFTotal());

                    setCellValue(row, 113, economize.getPF());
                    setCellValue(row, 114, economize.getPP());
                    setCellValue(row, 115, economize.getPG());
                    setCellValue(row, 116, economize.getPTotal());

                    setCellValue(row, 117, economize.getKF());
                    setCellValue(row, 118, economize.getKP());
                    setCellValue(row, 119, economize.getKG());
                    setCellValue(row, 120, economize.getKTotal());

                    setCellValue(row, 121, economize.getEleTotal());
                    setCellValue(row, 122, economize.getFactoryPrice());
                    setCellValue(row, 123, economize.getUpAccount());
                    setCellValue(row, 124, economize.getSPPrice());

                    setCellValue(row, 125, economize.getSPAccount());
//                    setCellValue(row,126,economize.getJJAverage());
//                    setCellValue(row,127,economize.getJJAccount());
                    setCellValue(row, 126, economize.getDHAverage());
                    setCellValue(row, 127, economize.getEleAccount());

                    /*****  实际执行情况合计  ****/
                    System.err.println("**********实际执行情况合计************");
                    System.err.println("电量：" + reality.getEleAccount());
                    setCellValue(row, 128, reality.getEleAccount());
                    setCellValue(row, 129, reality.getElePrice());
                    setCellValue(row, 130, reality.getEleF());
                    setCellValue(row, 131, reality.getJJPrice());
                    setCellValue(row, 132, reality.getZzJJPrice());
                    setCellValue(row, 133, reality.getDHPrice());
                    setCellValue(row, 134, reality.getDHAcount());


                    /*****  假设执行政府定价  ****/
                    // 电量
                    setCellValue(row, 135, assume.getFF());
                    setCellValue(row, 136, assume.getFP());
                    setCellValue(row, 137, assume.getFG());
                    setCellValue(row, 138, assume.getFTotal());

                    setCellValue(row, 139, assume.getPF());
                    setCellValue(row, 140, assume.getPP());
                    setCellValue(row, 141, assume.getPG());
                    setCellValue(row, 142, assume.getPTotal());

                    setCellValue(row, 143, assume.getKF());
                    setCellValue(row, 144, assume.getKP());
                    setCellValue(row, 145, assume.getKG());
                    setCellValue(row, 146, assume.getKTotal());
                    setCellValue(row, 147, assume.getEleAccountTotal());
                    // 电价
                    setCellValue(row, 148, assume.getFFP());
                    setCellValue(row, 149, assume.getFPP());
                    setCellValue(row, 150, assume.getFGP());

                    setCellValue(row, 151, assume.getPFP());
                    setCellValue(row, 152, assume.getPPP());
                    setCellValue(row, 153, assume.getPGP());

                    setCellValue(row, 154, assume.getKFP());
                    setCellValue(row, 155, assume.getKPP());
                    setCellValue(row, 156, assume.getKGP());
                    // 电费
                    setCellValue(row, 157, assume.getFFM());
                    setCellValue(row, 158, assume.getFPM());
                    setCellValue(row, 159, assume.getFGM());
                    setCellValue(row, 160, assume.getFTotalM());

                    setCellValue(row, 161, assume.getPFM());
                    setCellValue(row, 162, assume.getPPM());
                    setCellValue(row, 163, assume.getPGM());
                    setCellValue(row, 164, assume.getPTotalM());

                    setCellValue(row, 165, assume.getKFM());
                    setCellValue(row, 166, assume.getKPM());
                    setCellValue(row, 167, assume.getKGM());
                    setCellValue(row, 168, assume.getKTotalM());

                    setCellValue(row, 169, assume.getElePriceTotal());
                    setCellValue(row, 170, assume.getBasicsPrice());
                    setCellValue(row, 171, assume.getJJ());
                    setCellValue(row, 172, assume.getEleTotal());


                    /*****  优惠电费  ****/
                    setCellValue(row, 173, discount.getEleAccount());
                    setCellValue(row, 174, discount.getBasicsAccount());
                    setCellValue(row, 175, discount.getZzJJ());
                    setCellValue(row, 176, discount.getTotal());


                    rowIndex++;
                }

            }

            String filename = encodingFilename(sheetName);
            out = new FileOutputStream(getAbsoluteFile(filename));
            workbook.write(out);
            return AjaxResult.success(filename);
        } catch (Exception e) {
            throw new BusinessException("导出Excel失败，请联系网站管理员！" + e.getMessage());
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 设置excel值
     *
     * @param row
     * @param cellNum
     * @param obj
     */
    private void setCellValue(Row row, int cellNum, Object obj) {
        Cell cell = row.createCell(cellNum);
        if (obj instanceof BigDecimal) {
            cell.setCellValue(((BigDecimal) obj).stripTrailingZeros().toPlainString());
        } else
            cell.setCellValue(obj.toString());
    }

    /**
     * 设置数据字典的值
     *
     * @param row
     * @param cellNum
     * @param key
     * @param type
     */
    private void setCellValue(Row row, int cellNum, String key, String type) {
        String label = dictDataService.selectDictLabel(type, key);
        setCellValue(row, cellNum, label);
    }

    /**
     * 编码文件名
     */
    public String encodingFilename(String filename) {
        filename = UUID.randomUUID().toString() + "_" + filename + ".xls";
        return filename;
    }

    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    public String getAbsoluteFile(String filename) {
        String downloadPath = Global.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }

    /**
     * 根据basicsId获取丰 ，平 ，枯
     *
     * @param basicsId
     * @return
     */
    public String getMonth(Integer basicsId) {
        Basics basics = basicsService.selectBasicsById(basicsId);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(basics.getResultDate());
        int month = calendar.get(Calendar.MONTH) + 1;
        if (month == 6 || month == 7 || month == 8 || month == 9 || month == 10) {
            return "丰";
        } else if (month == 5 || month == 11) {
            return "平";
        } else {
            return "枯";
        }
    }

}
